<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-histories')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon @click="isDeleteShow = true" v-else name="delete" />
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onSearchItem(item, index)"
      ><van-icon name="close" v-show="isDeleteShow"
    /></van-cell>
  </div>
</template>

<script>
export default {
  neme: "SearchHistory",
  props: {
    //prpo数据
    //如果是普通的数据，绝对不能修改，
    // 即便修改了，也不会传导给父组件
    // 如果是引用数据类型，可以修改，[].push 不能重新赋值xxx=[]
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isDeleteShow: false, //控制删除显示状态
    };
  },
  methods: {
    onSearchItem(item, index) {
      if (this.isDeleteShow) {
        //删除状态
        this.searchHistories.splice(index, 1);
      } else {
        //非删除状态 点击进入搜索
        this.$emit("search", item);
      }
    },
  },
};
</script>

<style>
</style>